package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    /*
    * SpringMVC 传入参数：
    *   默认：QueryString方式来接受前端的参数
    *   如果是其他的方式传入参数：
    *       必要注解：
    *           路径的传入参数：@PathVariable
    *           请求体的json传入参数：@RequestBody
    * */
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {

        Long companyId = SecurityUtil.getCompanyId();

        PageVO pageVO = courseBaseService.queryCourseList(params, model,companyId);

        return pageVO;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {

        // 1.将vo转为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);

        // 2.获得companyid并赋值给dto
        Long companyId = SecurityUtil.getCompanyId();

        dto.setCompanyId(companyId);

        // 3.调用service并获得结果数据
        CourseBaseDTO result = courseBaseService.createCourseBase(dto);

        return result;
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.getCourseBaseById(courseBaseId,companyId);
    }


    @PutMapping("course")
    public CourseBaseDTO modifyCourseBaseById(@RequestBody CourseBaseVO baseVO) {


        // 1.将vo转为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(baseVO);

        // 2.获得companyid并赋值给dto
        Long companyId = SecurityUtil.getCompanyId();

        dto.setCompanyId(companyId);

        CourseBaseDTO result = courseBaseService.modifyCourseBaseById(dto);


        return result;
    }
}
